package com.jfeat.am.module.wegou.services.patch.impl;

import com.jfeat.am.common.exception.BusinessException;
import com.jfeat.am.config.properties.ThirdPartyProperties;
import com.jfeat.am.module.wegou.exception.WegouBizException;
import com.jfeat.am.module.wegou.kit.RestClient;
import com.jfeat.am.module.wegou.services.patch.QueryExchangeRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

/**
 * Created by Silent-Y on 2018/2/22.
 */
@Service
public class QueryExchangeRateServiceImpl implements QueryExchangeRateService {

    @Autowired
    ThirdPartyProperties thirdPartyProperties;

    RestClient rest = new RestClient();

    private static String URL = "http://web.juhe.cn:8080/finance/exchange/rmbquot?key=";

    @Override
    public ResponseEntity<String> getExchangeRate() throws Exception{
        String api = URL + thirdPartyProperties.getApiKey();
//        HttpEntity<RegisterModel> requestEntity = new HttpEntity<RegisterModel>(model, rest.getHeaders());
        ResponseEntity<String> responseEntity = rest.exchange(api, HttpMethod.GET, null, String.class);
        if (responseEntity.getStatusCode() != HttpStatus.OK){
            throw new BusinessException(WegouBizException.GET_EXCHANGE_RATE_FAILED.getCode(),WegouBizException.GET_EXCHANGE_RATE_FAILED.getMessage());
        }
        return responseEntity;
    }
}
